﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace EmptyWallGallery.Core.BLL
{
    public class VideosListView
    {
        public int VideoId { get; set; }
        public string VideoTitle { get; set; }
        public string VideoDesc { get; set; }
        public string VideoLink { get; set; }
        public string VideoImage { get; set; }
        public bool VideoStatus { get; set; }
        public int VideoView { get; set; }
        public Guid UserId { get; set; }
        public string UserName { get; set; }
        public DateTime CreateDate { get; set; }
    }

    public class Videos
    {
        public static Videos Instance
        {
            get { return new Videos(); }
        }

        public static VideosListView LoadDataFromListItem(tbl_Video item)
        {
            var objImage = new VideosListView
                               {
                                   VideoStatus = Convert.ToBoolean(item.VideoStatus),
                                   VideoTitle = item.VideoTitle,
                                   VideoImage = item.VideoImage,
                                   VideoDesc = item.VideoDesc,
                                   VideoId = item.VideoId,
                                   VideoLink = item.VideoLink,
                                   UserId = Guid.Parse(item.UserId.ToString()),
                                   VideoView = Convert.ToInt32(item.VideoView),
                                   CreateDate = Convert.ToDateTime(item.CreateDate)
                               };
            return objImage;
        }

        public bool Create(string title, string desc, string url, string image, bool status, string userId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objVideo = new tbl_Video
                                       {
                                           VideoDesc = desc,
                                           VideoImage = image,
                                           VideoLink = url,
                                           VideoStatus = status,
                                           VideoTitle = title,
                                           VideoView = 0,
                                           UserId = Guid.Parse(userId),
                                           CreateDate = DateTime.Now
                                       };

                    dc.tbl_Videos.InsertOnSubmit(objVideo);
                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(int videoId, string title, string desc, bool status)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objVideo = (dc.tbl_Videos.Where(p => p.VideoId == videoId)).Single();
                    objVideo.VideoDesc = desc;
                    objVideo.VideoStatus = status;
                    objVideo.VideoTitle = title;

                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool UpdateView(int videoId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objAnswer = (dc.tbl_Videos.Where(p => p.VideoId == videoId)).Single();
                    objAnswer.VideoView = objAnswer.VideoView + 1;

                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(int videoId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objVideo = dc.tbl_Videos.Where(p => p.VideoId == videoId);

                    dc.tbl_Videos.DeleteAllOnSubmit(objVideo);
                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public VideosListView GetById(int videoId)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var objVideo = new VideosListView();
                var query = (from p in dc.tbl_Videos where p.VideoId == videoId select p).SingleOrDefault();

                if (query != null)
                {
                    objVideo.VideoStatus = Convert.ToBoolean(query.VideoStatus);
                    objVideo.VideoId = query.VideoId;
                    objVideo.VideoTitle = query.VideoTitle;
                    objVideo.VideoImage = query.VideoImage;
                    objVideo.VideoDesc = query.VideoDesc;
                    objVideo.VideoLink = query.VideoLink;
                    objVideo.VideoView = Convert.ToInt32(query.VideoView);
                    objVideo.UserId = Guid.Parse(query.UserId.ToString());
                    objVideo.CreateDate = Convert.ToDateTime(query.CreateDate);
                }

                return objVideo;
            }
        }

        public List<VideosListView> GetTopVideo(int take)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var queryable = from p in dc.tbl_Videos
                                join aspnetUser in dc.aspnet_Users on p.UserId equals aspnetUser.UserId
                                where p.VideoStatus == false
                                orderby p.CreateDate descending
                                select new VideosListView
                                {
                                    VideoDesc = p.VideoDesc,
                                    VideoId = p.VideoId,
                                    VideoImage = p.VideoImage,
                                    VideoLink = p.VideoLink,
                                    VideoTitle = p.VideoTitle,
                                    VideoStatus = Convert.ToBoolean(p.VideoStatus),
                                    UserId = Guid.Parse(p.UserId.ToString()),
                                    CreateDate = Convert.ToDateTime(p.CreateDate),
                                    UserName = aspnetUser.UserName
                                };
                return queryable.Take(take).ToList();
            }            
        }

        public List<VideosListView> GetAll(bool status, bool isWhere)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var queryable = from p in dc.tbl_Videos join aspnetUser in dc.aspnet_Users on p.UserId equals aspnetUser.UserId 
                                where !isWhere || p.VideoStatus == status orderby p.CreateDate descending 
                                select new VideosListView
                                           {
                                               VideoDesc = p.VideoDesc,
                                               VideoId = p.VideoId,
                                               VideoImage = p.VideoImage,
                                               VideoLink = p.VideoLink,
                                               VideoTitle = p.VideoTitle,
                                               VideoStatus = Convert.ToBoolean(p.VideoStatus),
                                               VideoView = Convert.ToInt32(p.VideoView),
                                               UserId = Guid.Parse(p.UserId.ToString()),
                                               CreateDate = Convert.ToDateTime(p.CreateDate),
                                               UserName = aspnetUser.UserName
                                           };
                return queryable.ToList();
            }
        }
    }
}